/**
 * Created by wurenjie on 15/5/13.
 */
$(function(){
    $.feelywiki.init({
        "titleElem" : "#h-title",
        "wikiPrefix" : "/wiki/",
        "redirectParam" : "redirectfrom"
    });
    $(document).on("click",".sub-submit",function(){
        var threadid=$(this).next().val();
        var content=$(this).parent().siblings("textarea").val();
        addInFloorMessage(threadid,content);
    });
    $("#myForm").submit(function(){
        event.preventDefault();
        addMessage();
    });
    $(".sub-image").click(function(){
        popover_open("50","50");
    });
    $(".t-content").each(function(){
        var inner = $(this).html();
        $(this).html($.feelywiki.start(inner));
    });
    $("#insert-image-confirm").click(function(){
        var filename=$("#filename").val()
        if(/^http[s]{0,1}:\/\/.+\.(jpg|jpeg|gif|png)$/.test(filename)){
            var wikiArea=$("#sayTxt").val();
            console.log(wikiArea);
            wikiArea+="\n[["+filename+"]]\n";
            //$("#wikiEdit-textarea").val(wikiArea);
            $("#sayTxt").val(wikiArea);
            popover_close();
        }
        else{
            $("#insert-image-null").show().html("图片地址不合法").fadeOut(2000);
            return 0;
        }
    });


});
function inczan(floorid,thisS){
    $.getJSON("/forum/inczan.json",{
        id:floorid
    },function(data){
        $(thisS).parents(".sayTxt").find("#zanNum").html(data.result);
    })
}
function addMessage() {
    var threadid=$("#thread-id").val();
    console.log(threadid);
    var sayTxt = $("#wikiEdit-textarea").val();
    if (sayTxt == "") {
        $("#message").show().html("你总得说点什么吧.").fadeOut(1200);
        return false;
    }
    $.post("/forum/reply.json", {
        threadid:threadid,
        content: sayTxt
    }, function (data) {
        data=JSON.parse(data);
        if(data.result=="success"){
            popover_open_info("发帖成功");
            setTimeout(function(){
                window.location.reload();
            },500)
        }
        else(
            popover_open_info(data.info)
        )
    });
}
function addInFloorMessage(threadid,content){
    $.post("/forum/replyinfloor.json",{
        threadid:threadid,
        content:content
    },function(data){
        data=JSON.parse(data);
        if(data.result=="success"){
            popover_open_info("发帖成功");
            setTimeout(function(){
                window.location.reload();
            },500)
        }
        else(
            popover_open_info(data.info)
        )
    });
}